home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / test / test_long.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  10.2 KB  |  255 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. verbose
  5. join
  6. randint
  7. SHIFT = 15
  8. BASE = 2 ** SHIFT
  9. MASK = BASE - 1
  10. MAXDIGITS = 10
  11. special = map(long, [
  12.     0,
  13.     1,
  14.     2,
  15.     BASE,
  16.     BASE >> 1])
  17. special.append(0x5555555555555555L)
  18. special.append(0xAAAAAAAAAAAAAAAAL)
  19. p2 = 0x4L
  20. for i in range(2 * SHIFT):
  21.     special.append(p2 - 1)
  22.     p2 = p2 << 1
  23.  
  24. del p2
  25. special = special + map((lambda x: ~x), special) + map((lambda x: -x), special)
  26.  
  27. def check(ok, *args):
  28.     if not ok:
  29.         raise TestFailed, join(map(str, args), ' ')
  30.     
  31.  
  32.  
  33. def getran(ndigits):
  34.     if not __debug__ and ndigits > 0:
  35.         raise AssertionError
  36.     nbits_hi = ndigits * SHIFT
  37.     nbits_lo = (nbits_hi - SHIFT) + 1
  38.     answer = 0x0L
  39.     nbits = 0
  40.     r = int(random() * SHIFT * 2) | 1
  41.     while nbits < nbits_lo:
  42.         bits = (r >> 1) + 1
  43.         bits = min(bits, nbits_hi - nbits)
  44.         if __debug__:
  45.             if bits <= bits:
  46.                 pass
  47.             elif not bits <= SHIFT:
  48.                 raise AssertionError
  49.         nbits = nbits + bits
  50.         answer = answer << bits
  51.         if r & 1:
  52.             answer = answer | (1 << bits) - 1
  53.         
  54.         r = int(random() * SHIFT * 2)
  55.     if __debug__:
  56.         if nbits <= nbits:
  57.             pass
  58.         elif not nbits <= nbits_hi:
  59.             raise AssertionError
  60.     if random() < 0.5:
  61.         answer = -answer
  62.     
  63.     return answer
  64.  
  65.  
  66. def getran2(ndigits):
  67.     answer = 0x0L
  68.     for i in range(ndigits):
  69.         answer = answer << SHIFT | randint(0, MASK)
  70.     
  71.     return answer
  72.  
  73.  
  74. def test_division_2(x, y):
  75.     (q, r) = divmod(x, y)
  76.     (q2, r2) = (x / y, x % y)
  77.     check(q == q2, 'divmod returns different quotient than / for', x, y)
  78.     check(r == r2, 'divmod returns different mod than % for', x, y)
  79.     check(x == q * y + r, 'x != q*y + r after divmod on', x, y)
  80.     if y > 0:
  81.         None(check if r <= r else r < y, 'bad mod from divmod on', x, y)
  82.     elif r < r:
  83.         pass
  84.     
  85.     check(r <= 0, 'bad mod from divmod on', x, y)
  86.  
  87.  
  88. def test_division(maxdigits = MAXDIGITS):
  89.     print 'long / * % divmod'
  90.     digits = range(1, maxdigits + 1)
  91.     for lenx in digits:
  92.         x = getran(lenx)
  93.         for leny in digits:
  94.             if not getran(leny):
  95.                 pass
  96.             y = 0x1L
  97.             test_division_2(x, y)
  98.         
  99.     
  100.  
  101.  
  102. def test_bitop_identities_1(x):
  103.     check(x & 0 == 0, 'x & 0 != 0 for', x)
  104.     check(x | 0 == x, 'x | 0 != x for', x)
  105.     check(x ^ 0 == x, 'x ^ 0 != x for', x)
  106.     check(x & -1 == x, 'x & -1 != x for', x)
  107.     check(x | -1 == -1, 'x | -1 != -1 for', x)
  108.     check(x ^ -1 == ~x, 'x ^ -1 != ~x for', x)
  109.     check(x == ~~x, 'x != ~~x for', x)
  110.     check(x & x == x, 'x & x != x for', x)
  111.     check(x | x == x, 'x | x != x for', x)
  112.     check(x ^ x == 0, 'x ^ x != 0 for', x)
  113.     check(x & ~x == 0, 'x & ~x != 0 for', x)
  114.     check(x | ~x == -1, 'x | ~x != -1 for', x)
  115.     check(x ^ ~x == -1, 'x ^ ~x != -1 for', x)
  116.     None(check if 1 + ~x == 1 + ~x else 1 + ~x == ~(x - 1), 'not -x == 1 + ~x == ~(x-1) for', x)
  117.     for n in range(2 * SHIFT):
  118.         p2 = 0x2L ** n
  119.         check(x << n >> n == x, 'x << n >> n != x for', x, n)
  120.         check(x / p2 == x >> n, 'x / p2 != x >> n for x n p2', x, n, p2)
  121.         check(x * p2 == x << n, 'x * p2 != x << n for x n p2', x, n, p2)
  122.         0(check if (x >> n) << n == (x >> n) << n else (x >> n) << n == x & ~(p2 - 1), 'not x & -p2 == x >> n << n == x & ~(p2 - 1) for x n p2', x, n, p2)
  123.     
  124.  
  125.  
  126. def test_bitop_identities_2(x, y):
  127.     check(x & y == y & x, 'x & y != y & x for', x, y)
  128.     check(x | y == y | x, 'x | y != y | x for', x, y)
  129.     check(x ^ y == y ^ x, 'x ^ y != y ^ x for', x, y)
  130.     check(x ^ y ^ x == y, 'x ^ y ^ x != y for', x, y)
  131.     check(x & y == ~(~x | ~y), 'x & y != ~(~x | ~y) for', x, y)
  132.     check(x | y == ~(~x & ~y), 'x | y != ~(~x & ~y) for', x, y)
  133.     check(x ^ y == (x | y) & ~(x & y), 'x ^ y != (x | y) & ~(x & y) for', x, y)
  134.     check(x ^ y == x & ~y | ~x & y, 'x ^ y == (x & ~y) | (~x & y) for', x, y)
  135.     check(x ^ y == (x | y) & (~x | ~y), 'x ^ y == (x | y) & (~x | ~y) for', x, y)
  136.  
  137.  
  138. def test_bitop_identities_3(x, y, z):
  139.     check(x & y & z == x & y & z, '(x & y) & z != x & (y & z) for', x, y, z)
  140.     check(x | y | z == x | y | z, '(x | y) | z != x | (y | z) for', x, y, z)
  141.     check(x ^ y ^ z == x ^ y ^ z, '(x ^ y) ^ z != x ^ (y ^ z) for', x, y, z)
  142.     check(x & (y | z) == x & y | x & z, 'x & (y | z) != (x & y) | (x & z) for', x, y, z)
  143.     check(x | y & z == (x | y) & (x | z), 'x | (y & z) != (x | y) & (x | z) for', x, y, z)
  144.  
  145.  
  146. def test_bitop_identities(maxdigits = MAXDIGITS):
  147.     print 'long bit-operation identities'
  148.     for x in special:
  149.         test_bitop_identities_1(x)
  150.     
  151.     digits = range(1, maxdigits + 1)
  152.     for lenx in digits:
  153.         x = getran(lenx)
  154.         test_bitop_identities_1(x)
  155.         for leny in digits:
  156.             y = getran(leny)
  157.             test_bitop_identities_2(x, y)
  158.             test_bitop_identities_3(x, y, getran((lenx + leny) / 2))
  159.         
  160.     
  161.  
  162.  
  163. def slow_format(x, base):
  164.     if (x, base) == (0, 8):
  165.         return '0L'
  166.     
  167.     digits = []
  168.     sign = 0
  169.     if x < 0:
  170.         (sign, x) = (1, -x)
  171.     
  172.     while x:
  173.         (x, r) = divmod(x, base)
  174.         digits.append(int(r))
  175.     digits.reverse()
  176.     if not digits:
  177.         pass
  178.     digits = [
  179.         0]
  180.     return '-'[:sign] + {
  181.         8: '0',
  182.         10: '',
  183.         16: '0x' }[base] + join(map((lambda i: '0123456789ABCDEF'[i]), digits), '') + 'L'
  184.  
  185.  
  186. def test_format_1(x):
  187.     atol
  188.     for base, mapper in ((8, oct), (10, str), (16, hex)):
  189.         got = mapper(x)
  190.         expected = slow_format(x, base)
  191.         check(got == expected, mapper.__name__, 'returned', got, 'but expected', expected, 'for', x)
  192.         check(atol(got, 0) == x, 'atol("%s", 0) !=' % got, x)
  193.     
  194.  
  195.  
  196. def test_format(maxdigits = MAXDIGITS):
  197.     print 'long str/hex/oct/atol'
  198.     for x in special:
  199.         test_format_1(x)
  200.     
  201.     for i in range(10):
  202.         for lenx in range(1, maxdigits + 1):
  203.             x = getran(lenx)
  204.             test_format_1(x)
  205.         
  206.     
  207.  
  208.  
  209. def test_misc(maxdigits = MAXDIGITS):
  210.     print 'long miscellaneous operations'
  211.     import sys
  212.     hugepos = sys.maxint
  213.     hugeneg = -hugepos - 1
  214.     hugepos_aslong = long(hugepos)
  215.     hugeneg_aslong = long(hugeneg)
  216.     check(hugepos == hugepos_aslong, 'long(sys.maxint) != sys.maxint')
  217.     check(hugeneg == hugeneg_aslong, 'long(-sys.maxint-1) != -sys.maxint-1')
  218.     
  219.     try:
  220.         check(int(hugepos_aslong) == hugepos, 'converting sys.maxint to long and back to int fails')
  221.     except OverflowError:
  222.         raise TestFailed, 'int(long(sys.maxint)) overflowed!'
  223.  
  224.     
  225.     try:
  226.         check(int(hugeneg_aslong) == hugeneg, 'converting -sys.maxint-1 to long and back to int fails')
  227.     except OverflowError:
  228.         raise TestFailed, 'int(long(-sys.maxint-1)) overflowed!'
  229.  
  230.     x = hugepos_aslong + 1
  231.     
  232.     try:
  233.         int(x)
  234.         raise ValueError
  235.     except OverflowError:
  236.         pass
  237.     except:
  238.         raise TestFailed, "int(long(sys.maxint) + 1) didn't overflow"
  239.  
  240.     x = hugeneg_aslong - 1
  241.     
  242.     try:
  243.         int(x)
  244.         raise ValueError
  245.     except OverflowError:
  246.         pass
  247.     except:
  248.         raise TestFailed, "int(long(-sys.maxint-1) - 1) didn't overflow"
  249.  
  250.  
  251. test_division()
  252. test_bitop_identities()
  253. test_format()
  254. test_misc()
  255.